<?php
namespace app\admin\controller;

use app\common\controller\Admin;

class Auth extends Admin
{
    //定义模块字段列表
    protected function fields($action='create', $data=[])
    {
        //合并初始数据
        $data = DcArrayArgs(config('form_data.admin_auth_'.$action), $data);
        //合并表单字段
        return DcArrayArgs(config('form_fields.admin_auth_'.$action), \daicuo\Auth::fields($data));
    }
    
	public function save()
    {
        config('common.validate_name', config('validate_name.admin_auth_save'));
        
		if( !$op_id = \daicuo\Op::save(input('post.')) ){
			$this->error(\daicuo\Op::getError());
		}
        
        DcCache('auth_all', null);  
		$this->success(lang('success'));
	}
    
	public function delete()
    {
        $ids = input('id/a');
		if(!$ids){
			$this->error(lang('mustIn'));
		}
        
        foreach($ids as $id){
            \daicuo\Op::delete_id($id);
        }
        
        DcCache('auth_all', null);
        $this->success(lang('success'));
	}
    
	public function edit()
    {
		if(!$id = input('id/d',0)){
			$this->error(lang('mustIn'));
		}
        
        $data = \daicuo\Op::get_id($id, false);
        if( is_null($data) ){
            $this->error(lang('empty'));
        }
        $data = \daicuo\Op::data_value($data);
        
        $this->assign('query', $this->query);
        $this->assign('data', $data);
        $this->assign('fields', $this->formFields('edit', $data));
		return $this->fetch();
	}
	
	//修改一条规则到数据库
	public function update()
    {
        $data = input('post.');
        if(!$data['op_id']){
			$this->error(lang('mustIn'));
		}
        
        config('common.validate_name', config('validate_name.admin_auth_update'));
        
        $info = \daicuo\Op::update_id($data['op_id'], $data);
        if(is_null($info)){
            $this->error(\daicuo\Op::getError());
        }
        
        DcCache('auth_all', null);
        $this->success(lang('success'));
	}
    
	public function index()
    {
        if($this->request->isAjax()){
            $args = array();
            $args['cache']  = false;
            $args['field']  = 'op_id,op_name,op_value,op_module,op_controll,op_action,op_order,op_status';
            $args['sort']   = input('get.sortName/s','op_id');
            $args['order']  = input('get.sortOrder/s','desc');
            $args['where']  = DcWhereQuery(['op_module','op_controll','op_action','op_status'], 'eq', $this->query);
            $args['where']['op_action'] = ['eq', 'auth'];
            if( $this->query['searchText'] ){
                $args['where']['op_name|op_value'] = ['like','%'.DcHtml($this->query['searchText'].'%')];
            }
            $list = \daicuo\Op::all($args);
            if($list){
                return json($list);
            }
            return json([]);
		}
		//地址栏参数
        $this->assign('query', $this->query);
        //表单筛选字段
        $this->assign('fields', $this->formFields('index', $this->query));
        //表格列字段
        $this->assign('columns', $this->tableColumns('index', $this->query));
        //加载模板
		return $this->fetch();
	}
}